export function paperMove(paper) {
    if (!paper) return;

    // 设置默认的缩放范围
    const MIN_SCALE = 0.4;
    const MAX_SCALE = 2;
    const SCALE_STEP = 0.1;

    // 初始化画布大小和位置，但保持父容器大小
    const initializePaper = () => {
        // 获取当前容器大小
        const containerBounds = paper.el.getBoundingClientRect();

        // 保持画布大小与容器一致
        paper.setDimensions(containerBounds.width, containerBounds.height);

        // 重置缩放
        paper.scale(1);

        // 居中内容，但不改变画布大小
        paper.fitToContent({
            padding: 50,
            allowNewOrigin: 'any',
            minWidth: containerBounds.width,
            minHeight: containerBounds.height,
            useModelGeometry: true
        });

        // 获取内容边界
        const graphBBox = paper.model.getBBox();
        if (!graphBBox) return;

        // 计算合适的缩放比例
        const scaleX = (containerBounds.width * 0.8) / graphBBox.width;
        const scaleY = (containerBounds.height * 0.8) / graphBBox.height;
        const scale = Math.min(Math.max(Math.min(scaleX, scaleY), MIN_SCALE), MAX_SCALE);

        // 应用缩放
        paper.scale(scale);

        // 居中内容
        const tx = (containerBounds.width - graphBBox.width * scale) / 2 - graphBBox.x * scale;
        const ty = (containerBounds.height - graphBBox.height * scale) / 2 - graphBBox.y * scale;

        paper.translate(tx, ty);
    };

    // 添加鼠标滚轮缩放功能
    paper.on('blank:mousewheel', (evt, x, y, delta) => {
        evt.preventDefault();

        const currentScale = paper.scale().sx;
        const mousePosition = paper.clientToLocalPoint({ x: evt.clientX, y: evt.clientY });

        const newScale = currentScale + (delta > 0 ? SCALE_STEP : -SCALE_STEP);

        if (newScale >= MIN_SCALE && newScale <= MAX_SCALE) {
            const beta = currentScale / newScale;
            const ax = mousePosition.x * (beta - 1);
            const ay = mousePosition.y * (beta - 1);

            paper.scale(newScale);
            paper.translate(
                paper.translate().tx + ax * newScale,
                paper.translate().ty + ay * newScale
            );
        }
    });

    // 拖动相关变量
    let isDragging = false;
    let lastMousePosition = null;

    // 添加拖动功能
    paper.on('blank:pointerdown', (evt, x, y) => {
        isDragging = true;
        lastMousePosition = { x: evt.clientX, y: evt.clientY };
        paper.el.style.cursor = 'grabbing';
    });

    paper.on('blank:pointermove', (evt, x, y) => {
        if (!isDragging || !lastMousePosition) return;

        const dx = evt.clientX - lastMousePosition.x;
        const dy = evt.clientY - lastMousePosition.y;
        const currentTranslate = paper.translate();

        paper.translate(
            currentTranslate.tx + dx,
            currentTranslate.ty + dy
        );

        lastMousePosition = { x: evt.clientX, y: evt.clientY };
    });

    const endDragging = () => {
        isDragging = false;
        lastMousePosition = null;
        paper.el.style.cursor = 'default';
    };

    paper.on('blank:pointerup', endDragging);
    paper.el.addEventListener('mouseleave', endDragging);

    // 防止文本选择
    paper.el.addEventListener('selectstart', (evt) => {
        if (isDragging) {
            evt.preventDefault();
        }
    });

    // 优化性能：使用 requestAnimationFrame
    let rafId = null;
    const smoothTransform = (callback) => {
        if (rafId) {
            cancelAnimationFrame(rafId);
        }
        rafId = requestAnimationFrame(callback);
    };

    // 监听窗口大小变化，但保持容器大小比例
    const handleResize = () => {
        smoothTransform(() => {
            const containerBounds = paper.el.getBoundingClientRect();
            paper.setDimensions(containerBounds.width, containerBounds.height);
        });
    };

    window.addEventListener('resize', handleResize);

    // 延迟初始化以确保容器尺寸已经计算完成
    setTimeout(initializePaper, 100);

    // 清理函数
    return () => {
        window.removeEventListener('resize', handleResize);
        if (rafId) {
            cancelAnimationFrame(rafId);
        }
        paper.el.removeEventListener('mouseleave', endDragging);
        paper.el.removeEventListener('selectstart', evt => { });
    };
}